package com.liu.spider.component;

import com.liu.spider.proxy.spider.Ip66Spider;
import com.liu.spider.proxy.spider.KuaidailiSpider;
import com.liu.spider.service.ProxyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component("ProxySpiderTask")
@Slf4j
public class ProxySpiderTask {

    @Autowired
    Ip66Spider ip66Spider;
    @Autowired
    KuaidailiSpider kuaidailiSpider;
    @Autowired
    ProxyService proxyService;

    public void taskNoParams(){
        try {
            long start = System.currentTimeMillis();
            log.info("=====>>>>>开始抓取代理");
            List<String> results = ip66Spider.crawl();
            results.addAll(kuaidailiSpider.crawl());

            if (!CollectionUtils.isEmpty(results)) {
                for (String proxy : results) {
                    this.proxyService.save(proxy);
                }
            }
            log.info("<<<<<=====抓取代理结束，共耗时[{}]ms.", System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.error("抓取代理定时调度任务发生异常", e);
        }
    }

}
